AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")

include('shared.lua')

function ENT:Initialize()

	self.Entity:SetModel("models/Items/AR2_Grenade.mdl")
	self.Entity:PhysicsInit(SOLID_VPHYSICS)
	self.Entity:SetMoveType(MOVETYPE_VPHYSICS)
	self.Entity:SetSolid(SOLID_VPHYSICS)
	self.Entity:DrawShadow(false)
	self.Entity:SetCollisionGroup(COLLISION_GROUP_WEAPON)

	local phys = self.Entity:GetPhysicsObject()
	
	if (phys:IsValid()) then
		phys:Wake()
	end
	
	self.timer = CurTime() + 1.5
	self.solidify = CurTime() + 0.050
	self.Cookoff = CurTime() + 0.050
	
	if(self.Entity.TouchExplode == true) then
		self.timer = CurTime() + 1.0
	end
	local trail = util.SpriteTrail(self.Entity, 0, Color(255,0,0), false, 5, 1, 1, 1/(15+1)*0.5, "trails/laser.vmt")
end

function ENT:Think()
	if (ValidEntity(self.Entity:GetNWEntity("owner"))==false) then
		self.Entity:Remove()
	end
	if (self.solidify > CurTime()) then
		self.SetOwner(self.Entity)
		self.Entity:SetCollisionGroup(GROUP_NONE)
	end
	if self.timer < CurTime() then
	self.Entity:Explode()
	end
end

function ENT:Explode()
	util.BlastDamage(self.Entity, self.Entity:GetNWEntity("owner"), self.Entity:GetPos(), 256, 50)

	local effectdata = EffectData()
	effectdata:SetStart(self.Entity:GetPos())
	effectdata:SetOrigin(self.Entity:GetPos())
	effectdata:SetScale(1)
	util.Effect("HelicopterMegaBomb", effectdata)

	local effectdata2 = EffectData()
	effectdata2:SetStart(self.Entity:GetPos())
	effectdata2:SetOrigin(self.Entity:GetPos())
	effectdata2:SetScale(1)
	util.Effect("Explosion", effectdata2)

	self.Entity:EmitSound(Sound("weapons/hegrenade/explode"..math.random(3,5)..".wav"))
	self.Entity:Remove()
end

function ENT:PhysicsCollide()
		if(self.Entity.TouchExplode == true) then
		if(self.Cookoff > CurTime()) then self.Entity.TouchExplode = false return end
		self.Entity:Explode()
		end
end
